﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form" action="">
            <button type="submit" class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="download"> <i class="layui-icon">&#xe601;</i>模板下载</button>
            <button type="button" id="import" class="layui-btn layui-btn-sm layui-btn-primary"><i class="layui-icon">&#xe67c;</i>上传Excel</button>
        </form>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container" id="toolbar">
                <button type="button" id="NF-delete" name="NF-delete" class="layui-btn layui-btn-danger layui-btn-sm data-delete-btn layui-hide" lay-event="delete"> <i class="layui-icon">&#xe640;</i>删除选中</button>
                <button id="submit" class="layui-btn layui-btn-sm data-import-btn site-demo-active layui-hide" lay-event="save"><i class="layui-icon">&#xe605;</i>保存提交</button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script>
    layui.use(['jquery', 'form', 'table', 'upload', 'common', 'commonTable'], function () {
        var $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            upload = layui.upload,
            commonTable = layui.commonTable,
            soulTable = layui.soulTable,
            common = layui.common;
        var dataJson = [];
        commonTable.rendertable({
            elem: '#currentTableId',
            id: 'currentTableId',
            data: dataJson,
            height: 'full-75',
            limit: 99999,//每页数据 默认
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count'] //自定义分页布局
                , first: false //不显示首页
                , last: false //不显示尾页
            },
            search: false,
            cols: [[
                { type: 'checkbox', fixed: "left", width: 70 },
                { field: 'ErrorMsg', title: '错误信息', width: 150, sort: true, filter: true },
                { field: 'F_FullName', title: '岗位名称', width: 120, sort: true, filter: true },
                { field: 'F_EnCode', title: '岗位编号', width: 120, sort: true, filter: true },
                {
                    field: 'F_EnabledMark', title: '状态', width: 80, sort: true, filter: true,
                    templet: function (d) {
                        if (d.F_EnabledMark == true) {
                            return "<span class='layui-btn layui-btn-normal layui-btn-xs'>有效</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>无效</span>";
                        }
                    }
                },
                { field: 'F_Description', title: '备注', minWidth: 150, sort: true, filter: true },
            ]]
        });
        //普通图片上传
        var index;
        var uploadInst = upload.render({
            elem: '#import'
            , url: "/SystemOrganize/Duty/CheckFile" //改成您自己的上传接口
            , size: 1024
            , accept: 'file'
            , headers: { 'X-CSRF-TOKEN': "" }
            , before: function (obj) {
                if ($('[name=__RequestVerificationToken]').length > 0) {
                    // 1、动态添加headers中的参数
                    this.headers['X-CSRF-TOKEN'] = $('[name=__RequestVerificationToken]').val();
                }
                // 2、动态添加的其它参数
                //this.data.type = outerType
                index = parent.layer.load(0, {
                    shade: [0.5, '#000'], //0.1透明度的背景
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    //失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                    common.modalMsg(res.msg, "warning");
                    parent.layer.close(index);
                    return false;
                }
                dataJson = res.data;
                //执行重载
                table.reload('currentTableId', {
                    data: dataJson
                }, 'data');
                parent.layer.close(index);
            }
            , error: function () {
                //演示失败状态，并实现重传
                parent.layer.close(index);
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        wcLoading.close();
        //行点击事件监听，控制按钮显示
        var morerList = ["NF-delete"];//选中1条以上显示
        commonTable.tableRowClick("checkbox", "currentTableFilter", "currentTableId", null, morerList);
        //监听提交
        form.on('submit(download)', function (data) {
            window.open('/SystemOrganize/Duty/Download');
            return false;
        });
        //toolbar监听事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'save') {
                var postData = {};
                var cachedata = table.cache.currentTableId;
                if (cachedata.length == 0) {
                    common.modalMsg("未导入数据", "warning");
                    return false;
                }
                for (var i = 0; i < cachedata.length; i++) {
                    if (cachedata[i].F_EnabledMark == false) {
                        common.modalMsg("存在错误数据", "warning");
                        return false;
                    }
                }
                postData.listData = JSON.stringify(cachedata);
                common.submitForm({
                    url: '/SystemOrganize/Duty/ImportForm',
                    param: postData,
                    success: function () {
                        common.parentreload('data-search-btn');
                    }
                })
                return false;
            }
            else if (obj.event === 'delete') {
                var data = table.checkStatus('currentTableId').data;
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                common.modalConfirm("注：您确定要删除选中数据吗？", function (r) {
                    if (r) {
                        var index = parent.layer.load(0, {
                            shade: [0.5, '#000'], //0.1透明度的背景
                        });
                        try {
                            var cachedata = soulTable.cache['currentTableId'];
                            for (var i = 0; i < data.length; i++) {
                                for (let j = cachedata.length - 1; j >= 0; j--) {
                                    if (cachedata[j].F_Id === data[i].F_Id) {
                                        cachedata.splice(j, 1);
                                    }
                                }
                            }
                            table.reload('currentTableId', {
                                data: cachedata
                            });
                            entity = null;
                            common.modalMsg("操作成功", "success");
                        } catch (e) {
                            alert(e);
                        }
                        parent.layer.close(index);
                    }
                    else {
                        return false;
                    }
                });
            }
            return false;
        });
    });
</script>